package com.sq.universe.base.conf;

import ch.qos.logback.core.Layout;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.spi.DeferredProcessingAware;
import ch.qos.logback.core.status.ErrorStatus;
import cn.hutool.core.net.NetUtil;
import cn.hutool.setting.Setting;
import com.alibaba.fastjson.JSON;
import com.sq.universe.base.utils.ApplicationContextUtils;
import com.sq.universe.log.entity.LogCenterEntity;
import com.sq.universe.log.feign.LogFeign;

import java.util.ArrayList;
import java.util.List;

//public class CustomAppender extends AppenderSkeleton {
//
//    private String serverInfo;
//
//    public CustomAppender(Layout layout) {
//        this.setLayout(layout);
//    }
//
//    public CustomAppender() {
//    }
//
//    public CustomAppender(Layout layout, String serverInfo) throws IOException {
//
//        this.setLayout(layout);
//        this.serverInfo = serverInfo;
//    }
//
//    @Override
//    protected void append(LoggingEvent event) {
//        Setting setting = new Setting("tlog.setting");
//        LogFeign logFeign = ApplicationContextUtils.getBean(LogFeign.class); // Feign.builder().target(LogFeign.class, "http://universe-log/");
//        LogCenterEntity logCenterEntity = new LogCenterEntity();
//        logCenterEntity.setIp(JSON.toJSONString(NetUtil.localIpv4s()));
//        logCenterEntity.setMsg(this.layout.format(event));
//        logCenterEntity.setSystem(setting.getStr("applicationName"));
//        logFeign.receiveLog(logCenterEntity);
//        System.out.println("Hello, " + serverInfo + " : "+ this.layout.format(event) );
//
//    }
//
//    @Override
//    public void close() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public boolean requiresLayout() {
//        // TODO Auto-generated method stub
//        return false;
//    }
//
//
//    public String getServerInfo() {
//        return serverInfo;
//    }
//
//    public void setServerInfo(String serverInfo) {
//        this.serverInfo = serverInfo;
//    }
//}

public class CustomAppender<E> extends UnsynchronizedAppenderBase<E> {

    private static List<String> msgArr = new ArrayList<>();

    protected Encoder<E> encoder;

    private String serverInfo;

    public Encoder<E> getEncoder() {
        return this.encoder;
    }

    public void setEncoder(Encoder<E> encoder) {
        this.encoder = encoder;
    }


    public void start() {
        int errors = 0;
        if (this.encoder == null) {
            this.addStatus(new ErrorStatus("No encoder set for the appender named \"" + this.name + "\".", this));
            ++errors;
        }

        if (errors == 0) {
            super.start();
        }

    }


    public void setLayout(Layout<E> layout) {
        this.addWarn("This appender no longer admits a layout as a sub-component, set an encoder instead.");
        this.addWarn("To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.");
        this.addWarn("See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details");
        LayoutWrappingEncoder<E> lwe = new LayoutWrappingEncoder();
        lwe.setLayout(layout);
        lwe.setContext(this.context);
        this.encoder = lwe;
    }

    protected void append(E eventObject) {
        if (this.isStarted()) {
            this.subAppend(eventObject);
        }
    }

    protected void subAppend(E event) {
        if (this.isStarted()) {
            try {
                if (event instanceof DeferredProcessingAware) {
                    ((DeferredProcessingAware)event).prepareForDeferredProcessing();
                }
                String msg = new String(this.encoder.encode(event));
                List<String> arr = new ArrayList<>();
//                arr =  new ArrayList<>(CustomAppender.msgArr);
//                CustomAppender.msgArr.clear();
//                List<String> finalArr = arr;
//                new Thread(new Runnable() {
//
//                    @Override
//                    public void run() {
//                        Setting setting = new Setting("tlog.setting");
//                        if(setting.getBool("enable") == null || !setting.getBool("enable")){
//                            return ;
//                        }
//                        LogFeign logFeign = ApplicationContextUtils.getBean(LogFeign.class); // Feign.builder().target(LogFeign.class, "http://universe-log/");
//                        if(logFeign == null){
//                            CustomAppender.msgArr.add(msg);
//                            return;
//                        }
//                        LogCenterEntity logCenterEntity = new LogCenterEntity();
//                        logCenterEntity.setIp(JSON.toJSONString(NetUtil.localIpv4s()));
//                        logCenterEntity.setSystem(setting.getStr("applicationName"));
//                        if(finalArr.size() > 0){
//                            for(String _msg : finalArr){
//                                logCenterEntity.setMsg(_msg);
//                                logFeign.receiveLog(logCenterEntity);
//                            }
//                        }
//                        logCenterEntity.setMsg(msg);
//                        logFeign.receiveLog(logCenterEntity);
//                    }
//                }).start();
            } catch (Exception var3) {
                this.started = false;
                this.addStatus(new ErrorStatus("IO failure in appender", this, var3));
            }

        }
    }

    public String getServerInfo() {
        return serverInfo;
    }

    public void setServerInfo(String serverInfo) {
        this.serverInfo = serverInfo;
    }

}
